﻿using System;
using System.Linq;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using GiveAQuiz.Common;
using System.IO;
using System.ComponentModel.DataAnnotations;

namespace GiveAQuiz.Questions
{
  [PartCreationPolicy(CreationPolicy.NonShared)]
  [Export(typeof(IQuestionType))]
  public class EssayQuestionType : QuestionType
  {
    public EssayQuestionType()
    {
    }

    DataTemplate _editorTemplate = null;
    UIElement _answerPanel = null;
    Image _thumbnail = null;
    string _questionText;
    string _answerText;
   
    public override string DisplayName
    {
      get { return "Essay Question"; }
    }

    public override UIElement Thumbnail
    {
      get
      {
        if (_thumbnail == null)
        {
          _thumbnail = new Image()
            {
              Source = new BitmapImage(new Uri("/GiveAQuiz.Questions;component/Images/EssayQuestionThumb.png", UriKind.Relative))
            };
        }
        return _thumbnail;
      }
    }

    public override DataTemplate EditorTemplate
    {
      get
      {
        if (_editorTemplate == null)
        {
          // Using a Helper function to build a DataTemplate for our editor
          // Could create a DataTemplate for this in any way you want
          _editorTemplate = QuestionHelper.GenerateDataTemplateForControl(typeof(EssayQuestionEditor));
        }
        return _editorTemplate;
      }
    }

    public override UIElement AnswerPanel
    {
      get
      {
        if (_answerPanel == null)
        {
          _answerPanel = new EssayAnswerControl();
        }
        return _answerPanel;
      }
    }

    public override QuestionResult GetResult(string answerData)
    {
      return QuestionResult.Undetermined;
    }

    public override string GetAnswerText(string answerData)
    {
      return answerData;
    }

    // Because its a simple essay question, we can do a 
    // one to one mapping between our data and the string
    // format.  In other questions this would be more complex
    public override string QuestionData
    {
      get
      {
        return QuestionText;
      }
      set
      {
        QuestionText = value;
      }
    }

    public override string QuestionText
    {
      get { return _questionText; }
      set
      {
        if (value != _questionText)
        {
          _questionText = value;
          RaisePropertyChanged("QuestionText");
          UpdateDefinition();
        }
      }

    }

    public string AnswerText
    {
      get { return _answerText; }
      set
      {
        if (value != _answerText)
        {
          _answerText = value;
          RaisePropertyChanged("AnswerText");
          ValidateAnswer();
        }
      }
    }

    public override string GetAnswerData()
    {
      return AnswerText;
    }

    public override bool ValidateQuestion()
    {
      SetErrors("QuestionText", new string[] { });
      if (string.IsNullOrWhiteSpace(QuestionText))
      {
        SetError("QuestionText", "QuestionText is required.");
        return false;
      }

      return true;
    }

    public override bool ValidateAnswer()
    {
      if (string.IsNullOrWhiteSpace(AnswerText))
      {
        SetError("AnswerText", "Essay answer is required.");
        return false;
      }

      ClearErrors("AnswerText");

      return true;
    }


    public override string GetCorrectAnswerText()
    {
      // Correct answer is not applicable to this question.
      return "N/A";
    }
  }
}
